<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>js-宏任务和微任务 | lafen | lafen&#39;s personal blog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="JavaScript,learning,note">
    <meta name="description" content="概念JavaScript是一门单线程语言，同一时间内只能处理一件事情。 JavaScript事件循环既然js是单线程，处理任务是顺序执行的，那如果要处理的任务多了，就需要排队了，万一某一个任务一直没有处理完，那后面的任务就会一直堵塞，故任务需要区分为同步任务和异步任务  图中表示的意思是：  js在执行任务的时候，将区分同步任务和异步任务，同步任务进入主线程执行，异步任务进入Event Table">
<meta name="keywords" content="JavaScript,learning,note">
<meta property="og:type" content="article">
<meta property="og:title" content="js-宏任务和微任务">
<meta property="og:url" content="https://lafen.gitee.io/2019/09/27/js-宏任务和微任务/js-宏任务和微任务/index.html">
<meta property="og:site_name" content="lafen">
<meta property="og:description" content="概念JavaScript是一门单线程语言，同一时间内只能处理一件事情。 JavaScript事件循环既然js是单线程，处理任务是顺序执行的，那如果要处理的任务多了，就需要排队了，万一某一个任务一直没有处理完，那后面的任务就会一直堵塞，故任务需要区分为同步任务和异步任务  图中表示的意思是：  js在执行任务的时候，将区分同步任务和异步任务，同步任务进入主线程执行，异步任务进入Event Table">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://i.loli.net/2019/09/27/RhaAotvGlXZx4pu.png">
<meta property="og:image" content="https://i.loli.net/2019/09/27/PqCQnNscDrKRBHV.png">
<meta property="og:updated_time" content="2019-09-28T07:37:32.656Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="js-宏任务和微任务">
<meta name="twitter:description" content="概念JavaScript是一门单线程语言，同一时间内只能处理一件事情。 JavaScript事件循环既然js是单线程，处理任务是顺序执行的，那如果要处理的任务多了，就需要排队了，万一某一个任务一直没有处理完，那后面的任务就会一直堵塞，故任务需要区分为同步任务和异步任务  图中表示的意思是：  js在执行任务的时候，将区分同步任务和异步任务，同步任务进入主线程执行，异步任务进入Event Table">
<meta name="twitter:image" content="https://i.loli.net/2019/09/27/RhaAotvGlXZx4pu.png">
    
        <link rel="alternate" type="application/atom+xml" title="lafen" href="/atom.xml">
    
    <link rel="shortcut icon" href="/img/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide">
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">lafen</h5>
          <a href="mailto:1031822863@qq.com" title="1031822863@qq.com" class="mail">1031822863@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/">
                <i class="icon icon-lg icon-home"></i>
                home
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives">
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags">
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories">
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/zhongxiaotian" target="_blank">
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://www.weibo.com/" target="_blank">
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://gitee.com/lafen" target="_blank">
                <i class="icon icon-lg icon-link"></i>
                Gitee
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">js-宏任务和微任务</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">js-宏任务和微任务</h1>
        <h5 class="subtitle">
            
                <time datetime="2019-09-27T08:21:40.000Z" itemprop="datePublished" class="page-time">
  2019-09-27
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/JavaScript/">JavaScript</a><ul class="article-category-list-child"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/JavaScript/learning-note/">learning note</a></li></ul></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#概念"><span class="post-toc-number">1.</span> <span class="post-toc-text">概念</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#JavaScript事件循环"><span class="post-toc-number">2.</span> <span class="post-toc-text">JavaScript事件循环</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#setTimeout"><span class="post-toc-number">3.</span> <span class="post-toc-text">setTimeout</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#setInterval"><span class="post-toc-number">4.</span> <span class="post-toc-text">setInterval</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Promise与process-nextTick-callback"><span class="post-toc-number">5.</span> <span class="post-toc-text">Promise与process.nextTick(callback)</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#经典示例"><span class="post-toc-number">6.</span> <span class="post-toc-text">经典示例</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#我的分析"><span class="post-toc-number">6.1.</span> <span class="post-toc-text">我的分析</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#最后"><span class="post-toc-number">7.</span> <span class="post-toc-text">最后</span></a></li></ol>
        </nav>
    </aside>


<article id="post-js-宏任务和微任务/js-宏任务和微任务" class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">js-宏任务和微任务</h1>
        <div class="post-meta">
            <time class="post-time" title="2019-09-27 16:21:40" datetime="2019-09-27T08:21:40.000Z" itemprop="datePublished">2019-09-27</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/JavaScript/">JavaScript</a><ul class="article-category-list-child"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/JavaScript/learning-note/">learning note</a></li></ul></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style="display:none">
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><p><code>JavaScript</code>是一门<strong>单线程</strong>语言，同一时间内只能处理一件事情。</p>
<h3 id="JavaScript事件循环"><a href="#JavaScript事件循环" class="headerlink" title="JavaScript事件循环"></a>JavaScript事件循环</h3><p>既然<code>js</code>是单线程，处理任务是顺序执行的，那如果要处理的任务多了，就需要排队了，万一某一个任务一直没有处理完，那后面的任务就会一直堵塞，故任务需要区分为<strong>同步任务</strong>和<strong>异步任务</strong></p>
<p><img src="https://i.loli.net/2019/09/27/RhaAotvGlXZx4pu.png" alt="eventtask.png"></p>
<p>图中表示的意思是：</p>
<ul>
<li><code>js</code>在执行任务的时候，将区分<strong>同步任务</strong>和<strong>异步任务</strong>，同步任务进入主线程执行，异步任务进入<code>Event Table</code>，并<strong>注册</strong>回调函数</li>
<li>当指定的事情完成时，<code>Event Table</code>会将这个回调函数移入<code>Event Queue</code></li>
<li>主线程内的任务执行完毕为<strong>空</strong>时，会去<code>Event Queue</code>读取对应的回调函数进入主线程执行</li>
<li>然后不断<strong>重复</strong>上述过程，也就是常说的<code>Event Loop</code>（<strong>事件循环</strong>）</li>
</ul>
<p><strong>注</strong>：<code>js</code>引擎存在<code>monitoring process</code>进程，会持续不断检查主线程执行栈是否为空，一旦为空，就会去<code>Event Queue</code>那里检查是否有等待被调用的函数</p>
<p><strong>简单示例</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> data = []</span><br><span class="line">$.ajax(&#123;</span><br><span class="line">	url: <span class="string">'www.js.com'</span>,</span><br><span class="line">    data: data,</span><br><span class="line">    success: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    	<span class="built_in">console</span>.log(<span class="string">'请求成功'</span>)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'结束'</span>)</span><br></pre></td></tr></table></figure>
<p>以上是一段简易的<code>ajax</code>请求代码，看看<code>js</code>是如何执行的</p>
<ul>
<li><code>ajax</code>进入<code>Event Table</code>，并<strong>注册</strong>回调函数<code>success</code></li>
<li>执行<code>console.log</code>(‘结束’)</li>
<li><code>ajax</code>事件完成，回调函数<code>success</code>进入<code>Event Queue</code></li>
<li>主线程从<code>Event Queue</code>中读取回调函数<code>success</code>并执行</li>
</ul>
<p>原来<code>ajax</code>请求干了这么多事情0.0…</p>
<h3 id="setTimeout"><a href="#setTimeout" class="headerlink" title="setTimeout"></a>setTimeout</h3><p>常用于模拟耗时操作的<code>setTimeout</code>，有时也并不是很精准，写的延时<code>1000ms</code>执行，但是有时候却可能有些差别，先看以下实例</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> start = <span class="built_in">Date</span>.now()</span><br><span class="line">setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> end = <span class="built_in">Date</span>.now()</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'延时1秒后执行'</span>, end - start)</span><br><span class="line">&#125;, <span class="number">1000</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'我先执行'</span>)</span><br></pre></td></tr></table></figure>
<p>我们都知道，以上代码打印顺序如下</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 我先执行</span></span><br><span class="line"><span class="comment">// '延时1秒后执行' 1000</span></span><br></pre></td></tr></table></figure>
<p>看起来貌似没什么毛病，但是运行多几次就会发现，<code>1000</code>并不一直是<code>1000</code>，还可能会是<code>1001、1002</code>…</p>
<p>假如主线程中存在非常<strong>耗时</strong>的操作，例如以下伪代码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    task()</span><br><span class="line">&#125;, <span class="number">1000</span>)</span><br><span class="line">sleep(<span class="number">10000</span>) <span class="comment">// 假如这是一个非常耗时的函数</span></span><br></pre></td></tr></table></figure>
<p>此时，<code>setTimeout</code>的延时<code>1000ms</code>执行，就差别很多了，先看看是怎么执行的</p>
<ul>
<li><code>js</code>执行过程中遇到<code>setTimeout</code>，首先将<code>task()</code>放入<code>Event Table</code>并注册，计时开始</li>
<li>往下执行遇到<code>sleep</code>函数，但是<code>sleep</code>函数执行得很慢，计时还在继续</li>
<li>1秒到了，计时事件<code>setTimeout</code>完成，<code>task()</code>进入<code>Event Queue</code>，但是<code>sleep</code>函数还在占用主线程，还没执行完，只能继续等待</li>
<li><code>sleep</code>函数终于执行完毕，<code>task()</code>终于从<code>Event Queue</code>进入主线程执行</li>
</ul>
<p>现在我们知道此时的<code>setTimeout</code>的含义是：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">在<span class="number">1</span>秒之后，需要把task()加入到Event Queue中,</span><br><span class="line">等待主线程空闲时进入主线程执行,</span><br><span class="line">但是当主线程正在处理耗时操作时，那就只能等着,</span><br><span class="line">从而导致了延迟的时间远超过了<span class="number">1</span>秒</span><br></pre></td></tr></table></figure>
<p>那我们经常遇到的<code>setTimeout(fn, 0)</code>，是不是就会立即执行呢？</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">答案是不会的，fn依然会进入Event Queue中等待主线程空闲之后才能进入主线程执行</span><br></pre></td></tr></table></figure>
<p>如果主线程为空时，会达到0秒后执行吗？</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">实际上也不会，根据HTML的标准，最低是<span class="number">4</span>ms，可运行尝试</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> start = <span class="built_in">Date</span>.now()</span><br><span class="line">setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> end = <span class="built_in">Date</span>.now()</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'延时0秒？'</span>, end - start)</span><br><span class="line">&#125;, <span class="number">0</span>)</span><br></pre></td></tr></table></figure>
<h3 id="setInterval"><a href="#setInterval" class="headerlink" title="setInterval"></a>setInterval</h3><p>与<code>setTimeout</code>相似，<code>setInterval</code>是<strong>每隔指定时间</strong>会将回调函数放入<code>EventQueue</code>中，如果主线程在忙，也同样需要等待。</p>
<p>值得注意的是，放入<code>Event Queue</code>的时间间隔是指定的，但是如果主线程耗时操作大于或等于这个时间间隔，那就完全看不出有时间间隔执行了，伪代码示例如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">setInterval(fn, <span class="number">1000</span>) <span class="comment">// 每间隔1000ms将回调fn置入Event Queue</span></span><br><span class="line">sleep(<span class="number">1000</span>) <span class="comment">// 假如这个函数将耗时1000ms，以上的间隔1000ms执行则看不出是有间隔的执行了</span></span><br></pre></td></tr></table></figure>
<h3 id="Promise与process-nextTick-callback"><a href="#Promise与process-nextTick-callback" class="headerlink" title="Promise与process.nextTick(callback)"></a>Promise与process.nextTick(callback)</h3><p><code>Promise</code>的定义和功能不再赘述，不了解的读者可以学习一下阮一峰老师的<a href="https://link.juejin.im/?target=http%3A%2F%2Fes6.ruanyifeng.com%2F%23docs%2Fpromise" target="_blank" rel="noopener">Promise</a></p>
<p>而<code>process.nextTick(callback)</code>类似node.js版的”<code>setTimeout</code>“，在事件循环的下一次循环中调用 <code>callback</code> 回调函数。</p>
<p>除了<strong>同步任务</strong>和<strong>异步任务</strong>，我们需要对任务有更精细的定义：</p>
<ul>
<li><code>macro-task</code>(<strong>宏任务</strong>)：包含整体代码<code>script</code>、<code>setTimeout</code>、<code>setInterval</code></li>
<li><code>micro-task</code>(<strong>微任务</strong>)：<code>Promise</code>、<code>process.nextTick</code></li>
</ul>
<p><strong>宏任务</strong>和<strong>微任务</strong>会进入<strong>不同</strong>的<code>Event Queue</code>，<code>js</code>在执行过程中，进入<strong>整体代码（宏任务）</strong>，开始第一次循环，接着执行<strong>所有微任务</strong>，然后再次从宏任务开始执行任务队列，完毕之后再次执行所有微任务，依次循环</p>
<p>先看以下代码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'setTimeout'</span>)</span><br><span class="line">&#125;, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'promise'</span>)</span><br><span class="line">    resolve()</span><br><span class="line">&#125;).then(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'then'</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'console'</span>)</span><br></pre></td></tr></table></figure>
<ul>
<li>整体代码作为<strong>宏任务</strong>，进入主线程</li>
<li>先遇到<code>setTimeout</code>，将其回调函数注册后分发到<strong>宏任务</strong><code>Event Queue</code></li>
<li>然后遇到<code>new Promise</code>，立即执行其中的函数打印出<code>promise</code>，<code>then</code>函数则分发到<strong>微任务</strong><code>Event Queue</code></li>
<li>往下遇到<code>console.log()</code>，直接打印出<code>console</code></li>
<li>此时整体代码作为<strong>第一个宏任务执行结束</strong>，首先看看有哪些微任务？此时<strong>微任务</strong><code>Event Queue</code>中发现了<code>then</code>，取出执行，打印出<code>then</code></li>
<li>第一轮事件循环结束，开始第二轮循环，从宏任务<code>Event Queue</code>开始，此时发现宏任务<code>Event Queue</code>中有<code>setTimeout</code>对应的回调函数，立即执行</li>
<li>结束</li>
</ul>
<p><strong>事件循环、宏任务、微任务</strong>关系图如下：</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://i.loli.net/2019/09/27/PqCQnNscDrKRBHV.png" alt="eventloop.png" title="">
                </div>
                <div class="image-caption">eventloop.png</div>
            </figure>
<h3 id="经典示例"><a href="#经典示例" class="headerlink" title="经典示例"></a>经典示例</h3><p>检验是否掌握了<code>js</code>执行机制，看以下代码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">'1'</span>);</span><br><span class="line"></span><br><span class="line">setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'2'</span>);</span><br><span class="line">    process.nextTick(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'3'</span>);</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'4'</span>);</span><br><span class="line">        resolve();</span><br><span class="line">    &#125;).then(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'5'</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br><span class="line">process.nextTick(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'6'</span>);</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'7'</span>);</span><br><span class="line">    resolve();</span><br><span class="line">&#125;).then(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'8'</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'9'</span>);</span><br><span class="line">    process.nextTick(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'10'</span>);</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'11'</span>);</span><br><span class="line">        resolve();</span><br><span class="line">    &#125;).then(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'12'</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="我的分析"><a href="#我的分析" class="headerlink" title="我的分析"></a><strong>我的分析</strong></h4><p><strong>第一轮</strong></p>
<ul>
<li><p>整体代码作为<strong>宏任务</strong>进入主线程执行</p>
</li>
<li><p>首先遇到<code>console.log(&#39;1&#39;)</code>，直接打印，<strong><em>此时控制台输出</em></strong> <em>1</em></p>
</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">宏任务<code>Event Queue</code></th>
<th style="text-align:center">微任务<code>Event Queue</code></th>
<th style="text-align:center">控制台<code>console</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">-</td>
<td style="text-align:center">-</td>
<td style="text-align:center"><em>1</em></td>
</tr>
</tbody>
</table>
<ul>
<li>往下遇到<code>setTimeout</code>，没有设置延迟时间，则默认为<code>0</code>，将其<strong>回调函数</strong>注册到<code>Event Table</code>后立即分发至<strong>宏任务</strong><code>Event Queue</code>，至于回调函数中有什么，先不管，因为要等待主线程空闲了才能进入主线程去执行，此时的任务队列里面是这样的</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">宏任务<code>Event Queue</code></th>
<th style="text-align:center">微任务<code>Event Queue</code></th>
<th style="text-align:center">控制台<code>console</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">1. <code>setTimeout</code></td>
<td style="text-align:center">-</td>
<td style="text-align:center"><em>1</em></td>
</tr>
</tbody>
</table>
<ul>
<li>再往下遇到<code>process.nextTick</code>，将其注册后分发至<strong>微任务</strong><code>Event Queue</code>，此时</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">宏任务<code>Event Queue</code></th>
<th style="text-align:center">微任务<code>Event Queue</code></th>
<th style="text-align:center">控制台<code>console</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">1. <code>setTimeout</code></td>
<td style="text-align:center">1. <code>process.nextTick</code></td>
<td style="text-align:center"><em>1</em></td>
</tr>
</tbody>
</table>
<ul>
<li>然后往下遇到<code>new Promise</code>，直接执行里面代码打印，<strong><em>此时控制台输出</em></strong> <em>7</em>，再调用<code>resolve()</code>，将<code>then</code>分发至<strong>微任务</strong><code>Event Queue</code>中。此时<strong>微任务</strong><code>Event Queue</code>中包含： <code>process.nextTick</code>和<code>then</code>的回调</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">宏任务<code>Event Queue</code></th>
<th style="text-align:center">微任务<code>Event Queue</code></th>
<th style="text-align:center">控制台<code>console</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">1. <code>setTimeout</code></td>
<td style="text-align:center">1. <code>process.nextTick</code></td>
<td style="text-align:center"><em>1 7</em></td>
</tr>
<tr>
<td style="text-align:center">-</td>
<td style="text-align:center">2.       <code>then</code></td>
</tr>
</tbody>
</table>
<ul>
<li>最后遇到<code>setTimeout</code>，同样没有设置延迟时间，则默认为<code>0</code>，将其回调立即分发至<strong>宏任务</strong><code>Event Queue</code>，里面有什么，先不管，还没到他执行，注意区别于前一个<code>setTimeout</code></li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">宏任务<code>Event Queue</code></th>
<th style="text-align:center">微任务<code>Event Queue</code></th>
<th style="text-align:center">控制台<code>console</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">1. <code>setTimeout</code></td>
<td style="text-align:center">1. <code>process.nextTick</code></td>
<td style="text-align:center"><em>1 7</em></td>
</tr>
<tr>
<td style="text-align:center">2. <code>setTimeout</code></td>
<td style="text-align:center">2.       <code>then</code></td>
</tr>
</tbody>
</table>
<ul>
<li><p>至此，整体代码作为<strong>宏任务</strong>执行完毕，主线程此时空闲了，就会去看看<strong>微任务</strong><code>Event Queue</code>中是否有任务，如果有，则将其执行<strong>清空</strong>，此时发现<strong>微任务</strong><code>Event Queue</code>中包含两个任务，则依次将<code>process.nextTick</code>与<code>then</code>添加至主线程执行：</p>
<p>先执行<code>process.nextTick</code>的回调，<strong><em>此时控制台输出</em></strong> <em>6</em>，然后执行<code>then</code>，此时<strong><em>此时控制台输出</em></strong> <em>8</em></p>
</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">宏任务<code>Event Queue</code></th>
<th style="text-align:center">微任务<code>Event Queue</code></th>
<th style="text-align:center">控制台<code>console</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">1. <code>setTimeout</code></td>
<td style="text-align:center"></td>
<td style="text-align:center"><em>1 7 6 8</em></td>
</tr>
<tr>
<td style="text-align:center">2. <code>setTimeout</code></td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
<ul>
<li>清空完<strong>微任务</strong><code>Event Queue</code>后，至此，<strong>一轮事件循环已结束</strong>，主线程立即进入<strong>下一轮事件循环</strong>，下一轮的事件循环，依旧采用这样的思路进行分析</li>
</ul>
<p><strong>第二轮</strong></p>
<ul>
<li><p>主线程将<strong>宏任务</strong><code>Evenet Queue</code>中的第一个<code>setTimeout</code><strong>回调</strong>取出执行，首先遇到<code>console.log(&#39;2&#39;)</code>，直接打印，<strong><em>此时控制台输出</em></strong> <em>2</em></p>
</li>
<li><p>往下遇到<code>process.nextTick</code>，将其分发至<strong>微任务</strong><code>Event Queue</code>等待</p>
</li>
<li><p>往下遇到<code>new Promise</code>，直接执行其中代码，先打印 <em>4</em>，<strong><em>此时控制台输出</em></strong> <em>4</em>，然后执行<code>resolve()</code>，将<code>then</code>分发至<strong>微任务</strong><code>Event Queue</code></p>
</li>
<li><p>至此，第二轮事件循环中的<strong>宏任务</strong>执行完毕，现在看看任务队列中都有些什么</p>
</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">宏任务<code>Event Queue</code></th>
<th style="text-align:center">微任务<code>Event Queue</code></th>
<th style="text-align:center">控制台<code>console</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">2. <code>setTimeout</code></td>
<td style="text-align:center">1. <code>process.nextTick</code></td>
<td style="text-align:center"><em>1 7 6 8 2 4</em></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center">2. <code>then</code></td>
</tr>
</tbody>
</table>
<ul>
<li><p>此时主线程发现<strong>微任务</strong><code>Event Queue</code>中又有两个任务可执行，然后又依次调用执行并将其清空，首先执行<code>process.nextTick</code>，打印出 <em>3</em>，<strong><em>此时控制台输出</em></strong> <em>3</em></p>
</li>
<li><p>然后执行<code>then</code>，打印出 <em>5</em>，<strong><em>此时控制台输出</em></strong> <em>5</em></p>
</li>
<li><p><strong>微任务</strong><code>Event Queue</code>执行完毕，<strong>第二轮事件循环正式结束</strong>，此时任务队列如下</p>
</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">宏任务<code>Event Queue</code></th>
<th style="text-align:center">微任务<code>Event Queue</code></th>
<th style="text-align:center">控制台<code>console</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">2. <code>setTimeout</code></td>
<td style="text-align:center"></td>
<td style="text-align:center"><em>1 7 6 8 2 4 3 5</em></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
<p><strong>第三轮</strong></p>
<ul>
<li><p>主线程再次将<strong>宏任务</strong><code>Event Queue</code>中的第二个<code>setTimeout</code>取出执行，首先就遇到了打印 <em>9</em>，<strong><em>此时控制台输出</em></strong> <em>9</em></p>
</li>
<li><p>往下执行遇到<code>process.nextTick</code>，分发至<strong>微任务</strong><code>Event Queue</code>中等待</p>
</li>
<li><p>再往下遇到<code>new Promise</code>，执行其中代码，打印出 <em>11</em>，<strong><em>此时控制台输出</em></strong> <em>11</em>，执行<code>resolve()</code>，将<code>then</code>分发至<strong>微任务</strong><code>Event Queue</code>，<strong>宏任务</strong>执行完毕，此时的任务队列如下</p>
</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">宏任务<code>Event Queue</code></th>
<th style="text-align:center">微任务<code>Event Queue</code></th>
<th style="text-align:center">控制台<code>console</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center">1. <code>process.nextTick</code></td>
<td style="text-align:center"><em>1 7 6 8 2 4 3 5 9 11</em></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center">2. <code>then</code></td>
</tr>
</tbody>
</table>
<ul>
<li><p>此时主线程发现<strong>微任务</strong><code>Event Queue</code>中又有任务，于是有依次去取其中的任务到主线程上执行，首先执行<code>process.nextTick</code>，打印出 <em>10</em>，<strong><em>此时控制台输出</em></strong> <em>10</em>，然后执行<code>then</code>，打印出 <em>12</em>，<strong><em>此时控制台输出</em></strong> <em>12</em></p>
</li>
<li><p>至此第三轮事件循环也已结束，此时的宏任务Event Queue与微任务Event Queue中都已清空，此时控制台已打印出全部</p>
</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">宏任务<code>Event Queue</code></th>
<th style="text-align:center">微任务<code>Event Queue</code></th>
<th style="text-align:center">控制台<code>console</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"><em>1 7 6 8 2 4 3 5 9 11 10 12</em></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
<p>整段代码，进行了三次事件循环，才执行完毕，打印输出的顺序跟代码顺序毫无相干，可见：理解其中的事件循环机制多么的重要。</p>
<p>(请注意，<code>node</code>环境下的事件监听依赖<code>libuv</code>与前端环境不完全相同，输出顺序可能会有误差)</p>
<h3 id="最后"><a href="#最后" class="headerlink" title="最后"></a>最后</h3><ul>
<li><strong>javascript是一门单线程语言</strong>，不管是什么新框架新语法糖实现的所谓异步，其实都是用同步的方法去模拟的</li>
<li>事件循环<code>Event Loop</code>是<code>js</code>实现异步的一种方法，也是<code>js</code>的<strong>执行机制</strong></li>
<li>微任务和宏任务还有很多种类，比如<code>setImmediate</code>等等，执行都是有共同点的</li>
</ul>
<p>参考原文如下： </p>
<p><a href="https://juejin.im/post/59e85eebf265da430d571f89" target="_blank" rel="noopener">https://juejin.im/post/59e85eebf265da430d571f89</a></p>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2019-09-28T07:37:32.656Z" itemprop="dateUpdated">2019-09-28 15:37:32</time>
</span><br>


        
        关于学习类文章网上已经有非常多，而此处文章都是我在学习过程中整理摘录的笔记，参考文章可以点击文末的参考链接进行查阅
        
    </div>
    
    <footer>
        <a href="https://lafen.gitee.io">
            <img src="/img/avatar.jpg" alt="lafen">
            lafen
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/">JavaScript</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/learning/">learning</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/note/">note</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lafen.gitee.io/2019/09/27/js-宏任务和微任务/js-宏任务和微任务/&title=《js-宏任务和微任务》 — lafen&pic=https://lafen.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lafen.gitee.io/2019/09/27/js-宏任务和微任务/js-宏任务和微任务/&title=《js-宏任务和微任务》 — lafen&source=lafen's personal blog" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lafen.gitee.io/2019/09/27/js-宏任务和微任务/js-宏任务和微任务/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《js-宏任务和微任务》 — lafen&url=https://lafen.gitee.io/2019/09/27/js-宏任务和微任务/js-宏任务和微任务/&via=https://lafen.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lafen.gitee.io/2019/09/27/js-宏任务和微任务/js-宏任务和微任务/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2019/10/15/mpvue-vue-socketio使用实现即时通讯/mpvue-vue-socketio使用实现即时通讯/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">mpvue-vue-socketio使用实现即时通讯</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2019/09/11/mpvue-趟坑--主动获取子组件数据/mpvue-趟坑--主动获取子组件数据/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">mpvue-趟坑--主动获取子组件数据</h4>
      </a>
    </div>
  
</nav>



    




















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        打笔巨款？~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check" data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style="display:none">
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style="display:none">
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>lafen &copy; 2019 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lafen.gitee.io/2019/09/27/js-宏任务和微任务/js-宏任务和微任务/&title=《js-宏任务和微任务》 — lafen&pic=https://lafen.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lafen.gitee.io/2019/09/27/js-宏任务和微任务/js-宏任务和微任务/&title=《js-宏任务和微任务》 — lafen&source=lafen's personal blog" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lafen.gitee.io/2019/09/27/js-宏任务和微任务/js-宏任务和微任务/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《js-宏任务和微任务》 — lafen&url=https://lafen.gitee.io/2019/09/27/js-宏任务和微任务/js-宏任务和微任务/&via=https://lafen.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lafen.gitee.io/2019/09/27/js-宏任务和微任务/js-宏任务和微任务/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"positon":"right","width":150,"height":300},"mobile":{"show":true,"scale":0.8},"react":{"opacity":0.8,"opacityDefault":0.8,"opacityOnHover":0.6},"log":false});</script></body>
</html>
